﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MixScriptWeb.control;
using MixScriptWeb.Share;
using MixScript.BLL;
using MixScriptWeb.AppCode.gjips;
using MixScript.BLL.ApplAdmin;



namespace MixScriptWeb.Member
{
    public partial class fSecMatrix : cCommonWeb2 //System.Web.UI.Page
    {
        private static DataTable dtGrid;
        
        private static SortDirection sd;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                mn = (MainMenu2)this.Master;
                hideStatusBar();
                if (!Page.IsPostBack)
                {

                    FillDropDownList(ddlUser, "sy_user", "userid", "userid", true);
                    FillDropDownList(ddlGroupMenu, "vw_groupmenu", "modulname", "modulname", true);
                    ddlGroupMenu.SelectedIndex = 0;




                }

            }
            catch (Exception ex)
            {

                throw;
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                cReferences oref = new cReferences();
                string sout = "";
                //oref.getInquiry(ref dtGrid, new string[] { "", "", "", "", "", "", "", "", ddlUser.SelectedItem.Text }, "vw_privilege", out sout);
                cApplAdmin oAdmin = new cApplAdmin();
                oAdmin.getPriv(ref dtGrid, ddlUser.SelectedItem.Text,ddlGroupMenu.SelectedItem.Text);
                gvdetail.DataSource = dtGrid;
                gvdetail.DataBind();


            }
            catch (Exception ex)
            {

                throw;
            }
        }


        private void settodt(GridView grv)
        {
            try
            {
                int c = 0;

                foreach (GridViewRow gv in grv.Rows)
                {


                    CheckBox cb = (CheckBox)gv.Cells[0].FindControl("cbtick");
                    HiddenField hd = (HiddenField)gv.Cells[0].FindControl("hdmenu");
                    dtGrid.Rows[c]["nodeid"] = hd.Value;
                    if (cb.Checked)
                    {
                        dtGrid.Rows[c]["tick"] = "1";
                    }
                    else
                    {
                        dtGrid.Rows[c]["tick"] = "0";
                    }


                    c += 1;
                }

                dtGrid.AcceptChanges();

            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                settodt(gvdetail);

                if (ddlUser.SelectedItem.ToString() != string.Empty)
                {
                    cApplAdmin oAdm = new cApplAdmin();
                    oAdm.savePriv(dtGrid, ddlUser.SelectedItem.ToString(),ddlGroupMenu.SelectedItem.ToString()); 


                }

            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }

        }

        protected void gvdetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void gvdetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvdetail.PageIndex = e.NewPageIndex;
            //gvdetail.DataBind();
            bindGridview(gvdetail, dtGrid);

        }
        protected void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                bool b = ((CheckBox)sender).Checked;


                foreach (GridViewRow gr in gvdetail.Rows)
                {
                    CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbtick");
                    cb.Checked = b;
                }

            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void gvdetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (sd == SortDirection.Ascending)
            {
                sd = SortDirection.Descending;
            }
            else
            {
                sd = SortDirection.Ascending;
            }

            DataView dataView = new DataView(dtGrid);
            dataView.Sort = e.SortExpression + " " + ShortDirection(sd);

            bindGridview(gvdetail, dataView);
        }

        protected void ddlGroupMenu_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                cReferences oref = new cReferences();
                string sout = "";
                //oref.getInquiry(ref dtGrid, new string[] { "", "", "", "", "", "", "", "", ddlUser.SelectedItem.Text }, "vw_privilege", out sout);
                cApplAdmin oAdmin = new cApplAdmin();
                oAdmin.getPriv(ref dtGrid, ddlUser.SelectedItem.Text,ddlGroupMenu.SelectedItem.Text);
                gvdetail.DataSource = dtGrid;
                gvdetail.DataBind();


            }
            catch (Exception ex)
            {

                throw;
            }

        }
    }
}